package com.vastcom.tyauthcenter.service.impl;

import com.vastcom.tyauthcenter.dao.*;
import com.vastcom.tyauthcenter.model.*;
import com.vastcom.tyauthcenter.service.ManageService;
import com.vastcom.tyauthcenter.tools.Tools;
import com.vastcom.tyauthcenter.vo.NzTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private RolegroupDao rolegroupDao;

    @Autowired
    private RoleResourceDao roleResourceDao;

    @Autowired
    private RightResourceDao rightResourceDao;
    @Autowired
    private LoginticketDao loginticketDao;
    @Autowired
    private LoginlogDao loginlogDao;

    @Override
    public Member saveMember(Member member) {
        try {
            return memberDao.save(member);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteMember(String id) {

        try {
            memberDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Member getMember(String id) {
        Optional<Member> temp = memberDao.findById(id);


        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Member> getMemberListByDept(String dept) {
        return memberDao.getMembersByDept(dept);
    }

    @Override
    public List<Member> getMemberListByRole(String role) {
        return memberDao.getMembersByRolegroup(role);
    }

    @Override
    public Member getMemberByUserNameAndPass(String username, String pass) {
        List<Member> ml = memberDao.getMembersByNameAndPass(username, pass);

        if (ml.size() > 0) {
            return ml.get(0);
        } else {
            return null;
        }
    }


    @Override
    public boolean isExsistUserName(String username) {
        List<Member> ml = memberDao.getMemberListByUserName(username);


        return ml.size() > 0 ? true : false;
    }


    @Override
    public Dept saveDept(Dept dept) {
        try {
            return deptDao.save(dept);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean deleteDept(String id) {

        try {
            deptDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public Dept getDept(String deptid) {
        Optional<Dept> temp = deptDao.findById(deptid);
        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Dept> getDeptByPid(String pid) {
        return deptDao.getDeptsByPid(pid);
    }

    @Override
    public List<Dept> getRootDept() {
        return deptDao.getRootDept();
    }

    @Override
    public List<NzTreeVo> getDeptNzTreeByPid(String pid) {
        List<NzTreeVo> result = new ArrayList<>();
        List<Dept> dl = this.getDeptByPid(pid);
        if (dl.size() == 0) {
            return result;
        } else {
            for (Dept d : dl) {

                NzTreeVo v = new NzTreeVo();
                v.setTitle(d.getName());
                v.setKey(d.getDeptid());

                //v.setType("DEPT");
                List<NzTreeVo> child = new ArrayList<>();

                child.addAll(this.getDeptNzTreeByPid(d.getDeptid()));
                if (child.size() > 0) {
                    v.setChildren(child);
                    v.setExpanded(true);
                } else {
                    v.setExpanded(false);

                }
                result.add(v);


            }
        }


        return result;
    }


    @Override
    public Rolegroup saveRoleGroup(Rolegroup rolegroup) {
        try {
            return rolegroupDao.save(rolegroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteRoleGroup(String id) {
        try {
            rolegroupDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public Rolegroup getRoleGroup(String id) {
        Optional<Rolegroup> temp = rolegroupDao.findById(id);


        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Rolegroup> getRoleGroupList() {
        return rolegroupDao.findAll();
    }


    @Override
    public List<Roleresource> saveRoleResource(List<Roleresource> rl) {
        try {
            return roleResourceDao.saveAll(rl);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public boolean deleteRoleResources(List<Roleresource> rl) {

        try {
            roleResourceDao.deleteInBatch(rl);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Roleresource getRoleResource(String id) {
        Optional<Roleresource> temp = roleResourceDao.findById(id);
        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Roleresource> getRoleResourceByRole(String roleid) {
        return roleResourceDao.getRoleResourcesByRole(roleid);
    }

    @Override
    public List<Roleresource> getRoleResourceByRoleAndType(String role, String type) {
        return roleResourceDao.getRoleresourcesByRoleAndType(role, type);
    }

    @Override
    public List<Roleresource> getRoleResourceByResourceid(String resourceid) {
        return roleResourceDao.getRoleResourcesByResource(resourceid);
    }


    @Override
    public boolean haveRightByResourceIdAndRole(String resourcceid, String role) {
        List<Roleresource> rl = roleResourceDao.getRoleresourcesByResourceAndRole(resourcceid, role);
        if (rl.size() > 0) {
            return true;
        }
        return false;
    }
    @Override
    public Rightresource saveRightResource(Rightresource rightresource) {
        try {
            return rightResourceDao.save(rightresource);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteRightResource(String id) {
        try {
            rightResourceDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public Rightresource getRightResource(String id) {
        Optional<Rightresource> temp = rightResourceDao.findById(id);
        return temp.isPresent() ? temp.get() : null;
    }

    @Override
    public List<Rightresource> getRightResourceListByType(String type) {
        return rightResourceDao.getRightresourceByType(type);
    }

    @Override
    public List<Rightresource> getRightRightResourceListByPid(String pid) {
        return rightResourceDao.getRightresourcesByPid(pid);
    }

    @Override
    public List<NzTreeVo> getRightResourceNztreeViByPid(String pid) {
        List<NzTreeVo> result = new ArrayList<>();
        List<Rightresource> rl = this.getRightRightResourceListByPid(pid);
        if(rl.size()==0){
            return result;
        }else{
            for (Rightresource rightresource : rl) {
                NzTreeVo nzTreeVo = new NzTreeVo();
                nzTreeVo.setKey(rightresource.getResourceid());
                nzTreeVo.setTitle(rightresource.getName());
                List<NzTreeVo> rlchild = this.getRightResourceNztreeViByPid(rightresource.getResourceid());
                if(rlchild.size()>0){
                    nzTreeVo.setChildren(rlchild);
                    nzTreeVo.setExpanded(false);
                }else{
                    nzTreeVo.setExpanded(false);
                }

                result.add(nzTreeVo);
            }
        }


        return result;
    }

    @Override
    public List<NzTreeVo> getRightResourceNztreeViByPidForZyzc(String pid) {
        List<NzTreeVo> result = new ArrayList<>();
        List<Rightresource> rl = this.getRightRightResourceListByPid(pid);
        if(rl.size()==0){
            return result;
        }else{
            for (Rightresource rightresource : rl) {
                NzTreeVo nzTreeVo = new NzTreeVo();
                nzTreeVo.setKey(rightresource.getResourceid());
                nzTreeVo.setTitle(rightresource.getName());
                List<NzTreeVo> rlchild = this.getRightResourceNztreeViByPidForZyzc(rightresource.getResourceid());
                if(rlchild.size()>0){
                    nzTreeVo.setChildren(rlchild);
                    nzTreeVo.setExpanded(true);
                }else{
                    nzTreeVo.setExpanded(false);
                }

                result.add(nzTreeVo);
            }
        }


        return result;
    }

    @Override
    public Loginticket saveLoginticket(Loginticket loginticket) {
        try {
          return   loginticketDao.save(loginticket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Loginticket getLoginticketBYid(String id) {
        Optional<Loginticket> byId = loginticketDao.findById(id);

        return byId.isPresent()?byId.get():null;
    }


    @Override
    public String getCurrentUserId() {
        try {
            String userid= (String) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
            if(Tools.isNullOrSpace(userid)){
                return null;
            }
            else {
                return userid;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String getCurrentRole() {
        String role=null;
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            role = authority.getAuthority();

        }

        if(Tools.isNullOrSpace(role)){
            return null;
        }
        else{
            return role;
        }

    }

    @Override
    public Loginlog saveLoginlog(Loginlog loginlog) {
        try {
            Loginlog save = loginlogDao.save(loginlog);
            return save;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
